package cem.agony.ui.controller;

import java.io.File;

import cem.agony.AgonyTracker;

import javafx.fxml.FXML;
import javafx.scene.control.Dialogs;
import javafx.stage.FileChooser;

public class RootLayoutController
{
    private AgonyTracker mainApp;

    public void setMainApp( AgonyTracker mainApp )
    {
        this.mainApp = mainApp;
    }

    @FXML
    private void handleNew()
    {
        mainApp.getRiderData().clear();
        mainApp.setRiderFilePath( null );
    }

    @FXML
    private void handleOpen()
    {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter( "XML files (*.xml)",
                                                                                 "*.xml" );
        fileChooser.getExtensionFilters().add( extFilter );

        File file = fileChooser.showOpenDialog( mainApp.getPrimaryStage() );
        if ( file != null )
        {
            mainApp.loadRiderDataFromFile( file );
        }
    }

    @FXML
    private void handleSave()
    {
        File riderFile = mainApp.getRiderFilePath();
        if ( riderFile != null )
        {
            mainApp.saveRiderDataToFile( riderFile );
        }
        else
        {
            handleSaveAs();
        }
    }

    @FXML
    private void handleSaveAs()
    {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter( "XML files (*.xml)",
                                                                                 "*.xml" );
        fileChooser.getExtensionFilters().add( extFilter );

        File file = fileChooser.showSaveDialog( mainApp.getPrimaryStage() );
        if ( file != null )
        {
            if ( !file.getPath().endsWith( ".xml" ) )
            {
                file = new File( file.getPath() + ".xml" );
            }

            mainApp.saveRiderDataToFile( file );
        }
    }
    
    @FXML
    private void handleConfigureMap()
    {
        mainApp.showMapBuilderDialog();
    }
    
    @FXML
    private void handleEditRiders()
    {
    }

    @FXML
    private void handleAbout()
    {
        Dialogs.showInformationDialog( mainApp.getPrimaryStage(),
                                       "Author: Stephanie Lewis\n2013, version 1",
                                       "Agony Tracker",
                                       "About" );
    }

    @FXML
    private void handleExit()
    {
        System.exit( 0 );
    }
}